package com.zhou.framework.mail;

import com.zhou.util.StringTool;

import javax.activation.FileTypeMap;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ContentTypeMap {
    private static Map<String,String> typeMap = new HashMap<>(20);
    static {
        typeMap.put("doc","application/msword");
        typeMap.put("dot","application/msword");
        typeMap.put("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        typeMap.put("dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template");
        typeMap.put("docm","application/vnd.ms-word.document.macroEnabled.12");
        typeMap.put("dotm","application/vnd.ms-word.template.macroEnabled.12");
        typeMap.put("xls","application/vnd.ms-excel");
        typeMap.put("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        typeMap.put("xlsm","application/vnd.ms-excel.sheet.macroEnabled.12");
    }
    public static String getContentType(String prefix){
        String type = FileTypeMap.getDefaultFileTypeMap().getContentType(new File("1."+prefix));
        if(StringTool.isNotEmpty(prefix) && "application/octet-stream".equalsIgnoreCase(type)){
            String t = typeMap.get(prefix.toLowerCase());
            if(t != null){
                type = t;
            }
        }
        return type;
    }
}
